import { callContentMethodSync } from "./com"

export const unsafeWindow = new Proxy({}, {
    get(_target, p) {
        const val: UnsafeWindowProperty = callContentMethodSync('unsafeWindow', p);
        if (val.type === 'variable') {
            return val.data;
        } else if (val.type === 'function') {
            return new Proxy(val, {
                get() {
                    return `function ${String(val.name)}(args){...}`
                },
                apply(_target, _thisArg, argArray) {
                    return callContentMethodSync('callWinodowFunc', {
                        func: val.name,
                        args: argArray
                    })
                },
            })
        } else {
            return new Proxy(val, {
                get(_target, pp) {
                    const subVal: UnsafeWindowProperty = callContentMethodSync('unsafeWindow', String(val.name) + '.' + String(pp));
                    if (subVal.type === 'function') {
                        return new Proxy(val, {
                            get() {
                                return `function ${String(val.name)}(args){...}`
                            },
                            apply(_target, _thisArg, argArray) {
                                return callContentMethodSync('callWinodowFunc', {
                                    func: val.name,
                                    args: argArray
                                })
                            },
                        })
                    }
                },
                apply(_target, _thisArg, argArray) {
                    return callContentMethodSync('callWinodowFunc', {
                        func: val.name,
                        args: argArray
                    })
                },
            })
        }

    },
    set() {
        throw Error('此对象为仅读')
    },
})